<!--
 * @Author: Wushiqi
 * @Descripttion: 斐波那契数列
 * @Date: 2021-04-13 11:15:04
 * @LastEditor: Wushiqi
 * @LastEditTime: 2021-04-13 11:29:53
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>斐波那契数列</title>
</head>
<body>
  <script>
    // 求出第n项的数
    // [1, 1, 2, 3, 5, 8, 13, 21, 34]
    // 1.循环法
    function fb(n) {
      let res = [1, 1]
      // 循环的次数
      let t = n - 2
      while(t > 0) {
        const a1 = res[res.length - 1]
        const a2 = res[res.length - 2]
        n--
        t = n - 2
        res.push(a1 + a2)
      }
      console.log(res);
      return res[res.length - 1]
    }

    // 递归法
    // function fb1(n) {
    //   let res = [1, 1]
    //   if (n > 2) {
    //     console.log(fb());
    //   }
    // }
    console.log(fb(10));
  </script>
</body>
</html>